// hyb_yl/mycenter/pages/twitterApply/twitterApply.js
var app = getApp()
var citys = require('../../../../utils/citys.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 1,
    cityData: [],
    region: [],
    isindex: 1,
    ischeck: true,
    issuss: false,
    logo: '',
    array: [],
    index: 0,
    modes: [],
    indexs: 0,
    upload_picture_list: "",
    data_arr: "",
    state: '-1',
    lntroduction: "",
    hid: '',
    introduce: "",
    bigMask: false,
  },
  bindRegionChange(e) {
    console.log(e)
    var that = this
    wx.authorize({
      scope: 'scope.userLocation',
      complete: function (res) {
        console.log(res)
        wx.chooseLocation({
          success(str) {
            console.log(str)
            var latitude = str.latitude
            var longitude = str.longitude

            var key = 'V7DBZ-K7C22-SXXUJ-CDUE7-AM2LH-AEFCM';
            //发送请求通过经纬度反查地址信息
            var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + str.latitude + "," + str.longitude + "&key=" + key + "&get_poi=1";
            wx.request({
              url: getAddressUrl,
              success: function (result) {
                console.log(result)
                var province = result.data.result.address_component.province;
                var city = result.data.result.address_component.city;
                var district = result.data.result.address_component.district;
                var address = result.data.result.address_component.province + '-' + result.data.result.address_component.city + '-' + result.data.result.address_component.district;
                console.log(address)
                //查询机构入驻是的地区id
                app.util.request({
                  'url': 'entry/wxapp/Regin.getjigouid',
                  data: {
                    province: province,
                    city: city,
                    district: district
                  },
                  success: function (res) {
                    console.log(res)
                    that.setData({
                      cityid: res.data.city.id,
                      districtid: res.data.district.id,
                      provinceid: res.data.province.id
                    })
                  }
                })
                that.setData({
                  address: address,
                  lng: longitude,
                  lat: latitude
                })
              }
            })
          }
        })
      }
    })
  },
  bindPickerChange: function (e) {
    var that = this
    var list = that.data.list
    var index = e.detail.value
    console.log(e)
    this.setData({
      index: e.detail.value,
      level_name: list[index].level_name,
      grade: list[index].id,
    })
  },
  bindModesChange: function (e) {
    var that = this
    var orlist = that.data.orlist
    var indexs = e.detail.value
    this.setData({
      indexs: e.detail.value,
      name: orlist[indexs].name,
      groupid: orlist[indexs].id,
    })
  },
  gotkhome: function () {
    wx.navigateTo({
      url: '/hyb_yl/mycenter/pages/Organ/Organ',
    })
  },
  gotkagin: function () {
    this.setData({
      step: 1
    })
  },
  chooseimg: function () {
    var that = this;
    var uniacid = app.siteInfo.uniacid
    var data_arr = that.data.upload_picture_list;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          wx.uploadFile({
            url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Uploadback&m=hyb_yl',
            filePath: tempFilePaths[i],
            name: 'upfile',
            formData: [],
            success: function (res) {
              console.log(res)
              that.setData({
                upload_picture_list: res.data
              })
            }
          })
        }
      }
    })
  },
  formSubmit: function (e) {
    console.log(e);
    const that = this;
    const agentname = e.detail.value.agentname;
    const longitude = that.data.lng;
    const latitude = that.data.lat;
    const xxaddress = e.detail.value.xxaddress;
    var state = that.data.state
    if (state == '0') {
      wx.showModal({
        title: '提示',
        content: '资料审核中',
        success(res) {}
      })
      return
    }

    if (that.data.grade) {
      var grade = that.data.grade;
    } else {
      var list = that.data.list
      var grade = list[0].id
    }


    if (that.data.groupid) {
      var groupid = that.data.groupid;
    } else {
      var orlist = that.data.orlist
      var groupid = orlist[0].id
    }


    var realname = e.detail.value.realname
    const hospitaltel = e.detail.value.hospitaltel;
    var address = that.data.address

    var cityid = that.data.cityid ? that.data.cityid : e.detail.value.provinceid
    var districtid = that.data.districtid ? that.data.districtid : e.detail.value.districtid
    var provinceid = that.data.provinceid ? that.data.provinceid : e.detail.value.provinceid
    console.log(grade, groupid)
    var lntroduction = e.detail.value.lntroduction;
    var bank_num = e.detail.value.bank_num;
    var bank_user = e.detail.value.bank_user;
    var bank_name = e.detail.value.bank_name;
    if (that.data.upload_picture_list == "") {
      wx.showToast({
        title: '请您上传logo图片',
        icon: 'none'
      })
      return;
    } else if (agentname == "") {
      wx.showToast({
        title: '请输入机构名称',
        icon: 'none'
      })
      return;
    } else if (address == '') {
      wx.showToast({
        title: '请选择所在地',
        icon: 'none'
      })
      return;
    } else if (xxaddress == "") {
      wx.showToast({
        title: '请输入您的详细地址',
        icon: 'none'
      })
      return;
    } else if (lntroduction == '') {
      wx.showToast({
        title: '请输入简介',
        icon: 'none'
      })
      return;
    } else if (realname == '') {
      wx.showToast({
        title: '请输入负责人姓名',
        icon: 'none'
      })
      return;
    } else if (hospitaltel == '') {
      wx.showToast({
        title: '请输入您的手机号',
        icon: 'none'
      })
      return;
    } else if (!(/^1[34578]\d{9}$/.test(hospitaltel))) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return;
      if (hospitaltel.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'none'
        })
        return;
      }
    } else if (bank_num == '') {
      wx.showToast({
        title: '请输入您的银行卡号',
        icon: 'none'
      })
      return;
    } else if (bank_user == '') {
      wx.showToast({
        title: '请输入您的开户名',
        icon: 'none'
      })
      return;
    } else if (bank_name == '') {
      wx.showToast({
        title: '请输入您的开户行',
        icon: 'none'
      })
      return;
    }
    //修改资料
    if (state == '1' || state == '5') {
      wx.showModal({
        title: '提示',
        content: '确定修改资料吗',
        success(res) {
          if (res.confirm) {
            app.util.request({
              'url': 'entry/wxapp/regin.addjigou',
              data: {
                agentname: agentname,
                longitude: longitude,
                latitude: latitude,
                address: address,
                grade: grade,
                groupid: groupid,
                realname: realname,
                hospitaltel: hospitaltel,
                xxaddress: xxaddress,
                openid: wx.getStorageSync('openid'),
                username: agentname,
                logo: that.data.upload_picture_list,
                cityid: cityid,
                districtid: that.data.districtid,
                provinceid: that.data.provinceid,
                lntroduction: lntroduction,
                bank_num: bank_num,
                bank_user: bank_user,
                bank_name: bank_name,
                hid: that.data.hid,
                state: state
              },
              success: function (res) {
                console.log(res)
                wx.showToast({
                  title: '提交成功',
                  icon: 'none'
                })
                that.getIfjigou()
                setTimeout(function () {
                  that.setData({
                    step: 2
                  })
                }, 800)
              }
            })
          }
        }
      })
      return
    }
    //已暂停
    if (state == '2') {
      wx.showModal({
        title: '提示',
        content: '您的认证被暂停，请联系管理员',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      })
      return
    }
    if (state == '3') {
      wx.showModal({
        title: '提示',
        content: '认证到期是否续费',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      })
      return
    }
    app.util.request({
      'url': 'entry/wxapp/regin.addjigou',
      data: {
        agentname: agentname,
        longitude: longitude,
        latitude: latitude,
        address: address,
        grade: grade,
        groupid: groupid,
        realname: realname,
        hospitaltel: hospitaltel,
        xxaddress: xxaddress,
        openid: wx.getStorageSync('openid'),
        username: agentname,
        logo: that.data.upload_picture_list,
        cityid: cityid,
        districtid: that.data.districtid,
        provinceid: that.data.provinceid,
        lntroduction: lntroduction,
        bank_num: bank_num,
        bank_user: bank_user,
        bank_name: bank_name,
        hid: that.data.hid,
        state: state
      },
      success: function (res) {
        console.log(res)
        wx.showToast({
          title: '提交成功',
          icon: 'none'
        })
        that.getIfjigou()
        setTimeout(function () {
          that.setData({
            step: 2
          })
        }, 800)
      }
    })


  },
  checkboxChange: function (e) {
    console.log(e);
    const value = e.detail.value.length;
    if (value == 0) {
      this.setData({
        ischeck: false
      })
    } else {
      this.setData({
        ischeck: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var hid = options.hid;
    var tkopenid = options.openid
    var openid = wx.getStorageSync('openid')
    if (openid) {
      that.setData({
        bigMask: true,
      })
    }
    if (options.openid) {
      that.setData({
        tkopenid: tkopenid,
      })
    }
    if (hid != '' && hid != undefined && hid != 'undefined') {
      that.setData({
        hid: hid
      })
    }
    that.getIfjigou()
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getjigoudemgji()
    this.getjigoujurisdiction()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //读取机构级别
  getjigoudemgji() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/regin.getorganlevel',
      data: {},
      success: function (res) {
        console.log(res)
        that.setData({
          list: res.data,
        })
      }
    })
  },
  //查询机构权限
  getjigoujurisdiction() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/regin.jurisdiction',
      data: {},
      success: function (res) {
        console.log(res.data[0].id)
        that.setData({
          orlist: res.data,
        })
      }
    })
  },
  //查询是否有机构存在
  getIfjigou() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/user.hospital_detail',
      data: {
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {
        console.log(res)
        that.setData({
          detail: res.data,
          level_name: res.data.level_name,
          name: res.data.name,
          address: res.data.address,
          xxaddress: res.data.xxaddress,
          upload_picture_list: res.data.logo,
          provinceid: res.data.province,
          cityid: res.data.city,
          districtid: res.data.district,
          grade: res.data.grade,
          groupid: res.data.groupid,
          lng: res.data.longitude,
          lat: res.data.latitude,
          state: res.data.state,
          lntroduction: res.data.lntroduction
        })
      }
    })
  }
})